package com.fau.wm.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.validation.ConstraintViolation;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.fau.wm.entity.MedicalAdmin;
import com.fau.wm.entity.MedicalLogistics;
import com.fau.wm.service.ILogisticsManagerService;
import com.fau.wm.util.Page;
import com.fau.wm.util.ParamUtil;
import com.fau.wm.util.SerialNumUtil;
import com.fau.wm.util.ValidatorUtil;

@Service
@Transactional(readOnly = true)
public class LogisticsManagerService extends BaseService implements ILogisticsManagerService {

	public Page findLogisticsByPage(MedicalLogistics logistics,
			Map<String, Object> map) {

		StringBuilder countSql = new StringBuilder("select count(*) from MedicalLogistics a where 1=1 and a.hospitalId=? ");
		StringBuilder statusSql = new StringBuilder("from MedicalLogistics a  where 1=1 and a.hospitalId=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(logistics.getHospitalId());
		
		if(StringUtils.isNotBlank(logistics.getName())){
			countSql.append("and a.name like ? ");
			statusSql.append("and a.name like ? ");
			objectList.add("%"+logistics.getName()+"%");
		}
		if(StringUtils.isNotBlank(logistics.getConPeo())){
			countSql.append("and a.conPeo like ? ");
			statusSql.append("and a.conPeo like ? ");
			objectList.add("%"+logistics.getConPeo()+"%");
		}

		statusSql.append("order by a.createTime desc ");
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
		return page;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean saveLogistics(MedicalLogistics logistics, MedicalAdmin admin) {
		logistics.setSerialNum(SerialNumUtil.code());
		Set<ConstraintViolation<Object>> errorInfo = ValidatorUtil.validateObj(logistics);
		if(errorInfo.size()>0){
			return false;
		}else{
			String code = SerialNumUtil.code();
			logistics.setSerialNum(code);
			logistics.setHospitalId(admin.getEnterpriseId());
			return this.iBaseDao.saveObject(logistics);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void deleteLogistics(MedicalLogistics logistics) {
		String[] ids = logistics.getId().split("-");
		for (String id : ids) {
			this.iBaseDao.updateHql("delete from MedicalLogistics a where a.id=? ", id);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void saveEditLogistics(MedicalLogistics logistics) {
		MedicalLogistics  bean = (MedicalLogistics) this.iBaseDao.getObject(MedicalLogistics.class, logistics.getId());
		bean.setName(logistics.getName());
		bean.setAbbreviation(logistics.getAbbreviation());
		bean.setConPeo(logistics.getConPeo());
		bean.setCooperation(logistics.getCooperation());
		bean.setFax(logistics.getFax());
		bean.setGenCha(logistics.getGenCha());
		bean.setPhone(logistics.getPhone());
		bean.setRemark(logistics.getRemark());
		bean.setWebsite(logistics.getWebsite());
		
		this.iBaseDao.saveObject(bean);
	}
}
